home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7813 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to do a 32bit x 32bit multiplication
  5. Date: 27 Feb 1996 08:52:58 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gvctaINNe23@anvil.ugrad.cs.ubc.ca>
  8. References: <DnA53F.KnG.0.-s@hkusuc.hku.hk>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <DnA53F.KnG.0.-s@hkusuc.hku.hk>,
  12. Chow Cheuk Hung <h9316216@hkueee2.eee.hku.hk> wrote:
  13.  
  14.     Subject: How to do 32bit x 32bi multiplication
  15.  
  16.  
  17. The answer: use a 32-bit CPU.
  18.  
  19. Now, if the question was "how to do 32bit x 32bit multiplication, _and_ keep
  20. the full 64 bit result", I'd say:
  21.  
  22. The standard gives you a guaranteed ``minimum maximum'' size for an unsigned
  23. long integer of 2^32-1. This means that you can do 16x16->32 multiplications
  24. two unsigned shorts to get a result that can always fit into the unsigned long
  25. type.
  26.  
  27. Using such multiplications, you can build up a function or macro that does
  28. unsigned 32x32->64 multiplication, though it won't be very fast.
  29.  
  30. Another (non portable) way is to see if your compiler supports an extra
  31. precision long. For example, the GCC compiler has a "long long" type which
  32. holds 64 bits. This is convenient to work with, at the price of breaking with
  33. standard compliance.
  34.  
  35. To get a long long result from a multiplication, you have to cast both operands
  36. to long long, like this:
  37.  
  38.     long a = 3, b = 4;
  39.     long long b = (long long) a * (long long) b;
  40.  
  41. There is no automatic promotion from long to long long, hence the casts.
  42. Without the cast, the result will be chopped to 32 bits before being assigned
  43. to b.
  44.  
  45.  
  46. -- 
  47.  
  48.